//
//  ScrollView.m
//  Streaming
//
//  Created by 官琛 on 15-6-25.
//  Copyright (c) 2015年 liuxun. All rights reserved.
//

#import "ScrollView.h"

@implementation ScrollView

- (void)dealloc
{
    [self.title release];
    [self.image release];
    [self.releaseDate release];
    [self.releaseDate2 release];
    [self.wantedCount release];
    [self.director release];
    [self.actor release];
    [self.typeLabel release];
    [self.array release];
    [self.scrollView release];
    [self.com release];
    [self.scrollerImageView release];
    [self.myTimer release];
    
    [super dealloc];
    
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.scrollView.delegate = self;
        self.scrollView.backgroundColor = [UIColor whiteColor];
        self.scrollView.pagingEnabled = YES;
        self.scrollView.bounces = NO;
//        self.scrollView.showsHorizontalScrollIndicator = NO;
         self.array = [NSMutableArray array];
        [self addSubview:self.scrollView];
        [self afnGetRequest];
        
        
    }
    return self;
}

-(void)creatScroll5
{
     self.scrollerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * (self.array.count-3), 35, self.frame.size.width, self.frame.size.height - 35)];
    //    self.scrollerImageView.image = [UIImage imageNamed:@"yd__bg3.jpg"];
    [self.scrollView addSubview:self.scrollerImageView];
    //    self.scrollerImageView.layer.borderWidth = 1;
    //    self.scrollerImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    self.releaseDate = [[UILabel alloc]initWithFrame:CGRectMake(10+WIDTH*(self.array.count-3), 30, 100, 30)];
    self.releaseDate.textColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.releaseDate];
    [_releaseDate release];
    
    self.image = [[UIImageView alloc]initWithFrame:CGRectMake(10+WIDTH*(self.array.count-1), 60, 100, 150)];
    self.image.layer.shadowOpacity = 0.9;
    self.image.layer.shadowOffset = CGSizeMake(0, 3);
    self.image.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    self.image.layer.shadowRadius = 15;
    
    [self.scrollView addSubview:self.image];
    [_image release];
    
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(self.image.frame.origin.x+self.image.frame.size.width+10, self.releaseDate.frame.origin.y+20, 220, 1)];
    view1.backgroundColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:view1];
    view1.layer.shadowOpacity = 0.4;
    view1.layer.shadowOffset = CGSizeMake(10, 3);
    view1.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    view1.layer.shadowRadius = 15;
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(view1.frame.origin.x+view1.frame.size.width+5, view1.frame.origin.y+10, 1, 150)];
    view2.layer.shadowOpacity = 0.9;
    view2.layer.shadowOffset = CGSizeMake(0, 3);
    view2.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    view2.layer.shadowRadius = 15;
    view2.backgroundColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:view2];
    
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(view1.frame.origin.x, view1.frame.origin.y+view2.frame.size.height+15, 220, 1)];
    view3.backgroundColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:view3];
    [view1 release];
    [view2 release];
    [view3 release];
    
    self.title = [[UILabel alloc]initWithFrame:CGRectMake(self.image.frame.origin.x+self.image.frame.size.width+10, self.image.frame.origin.y, 200, 30)];
    self.title.font = [UIFont boldSystemFontOfSize:18];
    [self.scrollView addSubview:self.title];
    [_title release];
    
    self.wantedCount = [[UILabel alloc]initWithFrame:CGRectMake(self.title.frame.origin.x, self.title.frame.origin.y+self.title.frame.size.height+10, 40, 30)];
    self.wantedCount.textColor = [UIColor orangeColor];
    [self.scrollView addSubview:self.wantedCount];
    [_wantedCount release];
    
    UILabel *perLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.wantedCount.frame.origin.x+self.wantedCount.frame.size.width, self.wantedCount.frame.origin.y, 60, 30)];
    perLabel.textColor = [UIColor lightGrayColor];
    //    [perLabel setBackgroundColor:[UIColor redColor]];
    perLabel.text = @"人想看";
    [self.scrollView addSubview:perLabel];
    [perLabel release];
    
    self.typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(perLabel.frame.origin.x + perLabel.frame.size.width, perLabel.frame.origin.y, 110, 30)];
    self.typeLabel.textColor = [UIColor lightGrayColor];
    self.typeLabel.font = [UIFont boldSystemFontOfSize:17];
    //    [self.typeLabel setBackgroundColor:[UIColor redColor]];
    [self.scrollView addSubview:self.typeLabel];
    
    self.director = [[UILabel alloc]initWithFrame:CGRectMake(self.wantedCount.frame.origin.x, self.wantedCount.frame.origin.y+45, 150, 30)];
    self.director.textColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.director];
    [_director release];
    
    
    
    self.actor = [[UILabel alloc]initWithFrame:CGRectMake(self.director.frame.origin.x, self.director.frame.origin.y+30,150 , 30)];
    self.actor.textColor  = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.actor];
    [_actor release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(self.frame.origin.x * (self.array.count-3), 0, WIDTH, self.frame.size.height);
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:button];

    
}


-(void)creatScroll6
{
   

    

    self.scrollerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * (self.array.count-3), 35, self.frame.size.width, self.frame.size.height - 35)];
        //    self.scrollerImageView.image = [UIImage imageNamed:@"yd__bg3.jpg"];
        [self.scrollView addSubview:self.scrollerImageView];
        //    self.scrollerImageView.layer.borderWidth = 1;
        //    self.scrollerImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
        self.releaseDate = [[UILabel alloc]initWithFrame:CGRectMake(10+WIDTH*(self.array.count-3), 30, 100, 30)];
        self.releaseDate.textColor = [UIColor lightGrayColor];
        [self.scrollView addSubview:self.releaseDate];
        [_releaseDate release];
        
        self.image = [[UIImageView alloc]initWithFrame:CGRectMake(10+WIDTH*(self.array.count-1), 60, 100, 150)];
        self.image.layer.shadowOpacity = 0.9;
        self.image.layer.shadowOffset = CGSizeMake(0, 3);
        self.image.layer.shadowColor = [UIColor lightGrayColor].CGColor;
        self.image.layer.shadowRadius = 15;
        
        [self.scrollView addSubview:self.image];
        [_image release];
        
        UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(self.image.frame.origin.x+self.image.frame.size.width+10, self.releaseDate.frame.origin.y+20, 220, 1)];
        view1.backgroundColor = [UIColor lightGrayColor];
        [self.scrollView addSubview:view1];
        view1.layer.shadowOpacity = 0.4;
        view1.layer.shadowOffset = CGSizeMake(10, 3);
        view1.layer.shadowColor = [UIColor lightGrayColor].CGColor;
        view1.layer.shadowRadius = 15;
        
        UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(view1.frame.origin.x+view1.frame.size.width+5, view1.frame.origin.y+10, 1, 150)];
        view2.layer.shadowOpacity = 0.9;
        view2.layer.shadowOffset = CGSizeMake(0, 3);
        view2.layer.shadowColor = [UIColor lightGrayColor].CGColor;
        view2.layer.shadowRadius = 15;
        view2.backgroundColor = [UIColor lightGrayColor];
        [self.scrollView addSubview:view2];
        
        UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(view1.frame.origin.x, view1.frame.origin.y+view2.frame.size.height+15, 220, 1)];
        view3.backgroundColor = [UIColor lightGrayColor];
        [self.scrollView addSubview:view3];
        [view1 release];
        [view2 release];
        [view3 release];
        
        self.title = [[UILabel alloc]initWithFrame:CGRectMake(self.image.frame.origin.x+self.image.frame.size.width+20, self.image.frame.origin.y, 200, 30)];
        self.title.font = [UIFont boldSystemFontOfSize:18];
        [self.scrollView addSubview:self.title];
        [_title release];
        
        self.wantedCount = [[UILabel alloc]initWithFrame:CGRectMake(self.title.frame.origin.x, self.title.frame.origin.y+self.title.frame.size.height+10, 40, 30)];
        self.wantedCount.textColor = [UIColor orangeColor];
        [self.scrollView addSubview:self.wantedCount];
        [_wantedCount release];
        
        UILabel *perLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.wantedCount.frame.origin.x+self.wantedCount.frame.size.width, self.wantedCount.frame.origin.y, 60, 30)];
        perLabel.textColor = [UIColor lightGrayColor];
        //    [perLabel setBackgroundColor:[UIColor redColor]];
        perLabel.text = @"人想看";
        [self.scrollView addSubview:perLabel];
        [perLabel release];
        
        self.typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(perLabel.frame.origin.x + perLabel.frame.size.width, perLabel.frame.origin.y, 110, 30)];
        self.typeLabel.textColor = [UIColor lightGrayColor];
        self.typeLabel.font = [UIFont boldSystemFontOfSize:17];
        //    [self.typeLabel setBackgroundColor:[UIColor redColor]];
        [self.scrollView addSubview:self.typeLabel];
        
        self.director = [[UILabel alloc]initWithFrame:CGRectMake(self.wantedCount.frame.origin.x, self.wantedCount.frame.origin.y+30, 150, 30)];
        self.director.textColor = [UIColor lightGrayColor];
        [self.scrollView addSubview:self.director];
        [_director release];
        
        
        
        self.actor = [[UILabel alloc]initWithFrame:CGRectMake(self.director.frame.origin.x, self.director.frame.origin.y+30,150 , 30)];
        self.actor.textColor  = [UIColor lightGrayColor];
        [self.scrollView addSubview:self.actor];
        [_actor release];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(self.frame.origin.x * (self.array.count-3), 0, WIDTH, self.frame.size.height);
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//        button.layer.cornerRadius = 15;
//        button.layer.borderWidth = 1;
    
//        button.layer.backgroundColor = [UIColor orangeColor].CGColor;
    
        [self.scrollView addSubview:button];

    
    
    
    


}

-(void)creatScrollPlus
{
    
    self.scrollerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * (self.array.count-1), 35, self.frame.size.width, self.frame.size.height - 35)];
    
    //    self.scrollerImageView.image = [UIImage imageNamed:@"yd__bg3.jpg"];
    [self.scrollView addSubview:self.scrollerImageView];
    //    self.scrollerImageView.layer.borderWidth = 1;
    //    self.scrollerImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    self.releaseDate = [[UILabel alloc]initWithFrame:CGRectMake(10+WIDTH*(self.array.count-1), 30, 100, 30)];
    self.releaseDate.textColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.releaseDate];
    [_releaseDate release];
    
    self.image = [[UIImageView alloc]initWithFrame:CGRectMake(10+WIDTH*(self.array.count-1), 60, 100, 150)];
    self.image.layer.shadowOpacity = 0.9;
    self.image.layer.shadowOffset = CGSizeMake(0, 3);
    self.image.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    self.image.layer.shadowRadius = 15;
    
    [self.scrollView addSubview:self.image];
    [_image release];
    
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(self.image.frame.origin.x+self.image.frame.size.width+10, self.releaseDate.frame.origin.y+20, 260, 1)];
    view1.backgroundColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:view1];
    view1.layer.shadowOpacity = 0.4;
    view1.layer.shadowOffset = CGSizeMake(10, 3);
    view1.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    view1.layer.shadowRadius = 15;
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(view1.frame.origin.x+view1.frame.size.width+5, view1.frame.origin.y+10, 1, 150)];
    view2.layer.shadowOpacity = 0.9;
    view2.layer.shadowOffset = CGSizeMake(0, 3);
    view2.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    view2.layer.shadowRadius = 15;
    view2.backgroundColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:view2];
    
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(view1.frame.origin.x, view1.frame.origin.y+view2.frame.size.height+15, view1.frame.size.width, 1)];
    view3.backgroundColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:view3];
    [view1 release];
    [view2 release];
    [view3 release];
    
    self.title = [[UILabel alloc]initWithFrame:CGRectMake(self.image.frame.origin.x+self.image.frame.size.width+20, self.image.frame.origin.y, 200, 30)];
    self.title.font = [UIFont boldSystemFontOfSize:18];
    [self.scrollView addSubview:self.title];
    [_title release];
    
    self.wantedCount = [[UILabel alloc]initWithFrame:CGRectMake(self.title.frame.origin.x, self.title.frame.origin.y+self.title.frame.size.height+10, 40, 30)];
    self.wantedCount.textColor = [UIColor orangeColor];
    [self.scrollView addSubview:self.wantedCount];
    [_wantedCount release];
    
    UILabel *perLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.wantedCount.frame.origin.x+self.wantedCount.frame.size.width, self.wantedCount.frame.origin.y, 60, 30)];
    perLabel.textColor = [UIColor lightGrayColor];
    //    [perLabel setBackgroundColor:[UIColor redColor]];
    perLabel.text = @"人想看";
    [self.scrollView addSubview:perLabel];
    [perLabel release];
    
    self.typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(perLabel.frame.origin.x + perLabel.frame.size.width, perLabel.frame.origin.y, 110, 30)];
    self.typeLabel.textColor = [UIColor lightGrayColor];
    self.typeLabel.font = [UIFont boldSystemFontOfSize:17];
    //    [self.typeLabel setBackgroundColor:[UIColor redColor]];
    [self.scrollView addSubview:self.typeLabel];
    
    self.director = [[UILabel alloc]initWithFrame:CGRectMake(self.wantedCount.frame.origin.x, self.wantedCount.frame.origin.y+30, 150, 30)];
    self.director.textColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.director];
    [_director release];
    
    
    
    self.actor = [[UILabel alloc]initWithFrame:CGRectMake(self.director.frame.origin.x, self.director.frame.origin.y+30,150 , 30)];
    self.actor.textColor  = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.actor];
    [_actor release];
    
}

-(void)buttonAction:(UIButton *)button
{
    NSLog(@"2");
}


//-(void)createTimer
//{
//    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAciton:) userInfo:nil repeats:YES];
//}
//
//#pragma mark --定时器调用的方法
//- (void)timerAciton:(NSTimer *)timer{
//    
//    //当正常滚动的时候
//    self.page ++;
//    //将要到达偏移量的宽度
//    CGFloat offWith = _scrollView.frame.size.width *self.page;
//    NSLog(@"with = %f",offWith);
//    
//    //    [UIView animateWithDuration:0.5 animations:^{ //用动画也能实现,动画的偏移
//    [self.scrollView setContentOffset:CGPointMake(offWith, 0) animated:YES];
//    NSLog(@"正常滚动");
//    //    }];
//    
//    
//    //当不是正常滚动,滚动到边缘就取消动画
//    NSInteger number = self.scrollView.contentSize.width / self.scrollView.frame.size.width;
//    //number是当前图片个数
//    if (offWith == self.scrollView.frame.size.width * (number - 1)) {
//        self.page = 1;
//        [self.scrollView setContentOffset:CGPointMake(0, 0)];
//        NSLog(@"快速跳转");
//    }
//    
//}
//
//#pragma mark-- 停止定时器
//-(void)stopTimer
//{
//    if (self.myTimer) {
//        
//        if (self.myTimer.isValid) {//如果是开启状态
//            
//            [self.myTimer invalidate];
//            self.myTimer = nil;
//        }
//        
//    }
//    
//}
//
//#pragma mark--手指将要拖拽的时候停止计时器
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
//{
//    [self stopTimer];
//    NSLog(@"timer = %@",self.myTimer);
//}
//#pragma mark--将要结束拖拽,也就是手指离开的时候
//-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
//{
//    [self createTimer];
//    
//}
//
//#pragma mark --加速结束的时候
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    
//    
//    CGSize  size = scrollView.contentSize;
//    
//    CGFloat with = scrollView.frame.size.width;
//    self.page = scrollView.contentOffset.x / with;
//    
//    //往左划,如果是滑到最后一张图
//    if (scrollView.contentOffset.x == (size.width - with)) {
//        
//        self.page = 1;
//        [scrollView setContentOffset:CGPointMake(with, 0)];
//        
//    }
//    //往右滑,如果滑到第一张图
//    if (scrollView.contentOffset.x == 0){
//        
//        self.page = scrollView.contentSize.width / scrollView.frame.size.width - 2 ;
//        [scrollView setContentOffset:CGPointMake(size.width - with * 2, 0)];
//    }
//    
//    
//}
//
//
//#pragma mark tapAction;
//- (void)tapAction:(UITapGestureRecognizer *)tap{
//    
//    NSLog(@"page = %ld",self.page);
//    
//    //获得点击的视图
//    UIImageView *image = (UIImageView *)tap.view;
//    NSLog(@"image = %ld",image.tag);
//    
//    return;
//    
//}
//
//


-(void)afnGetRequest
{
   
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = @"http://api.m.mtime.cn/Movie/MovieComingNew.api?locationId=729";
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        //                    NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        
        NSMutableArray *array = [NSMutableArray array];
        array = [responseObject objectForKey:@"attention"];
       
        for (NSMutableDictionary *dic in array) {
            Coming *com = [[Coming alloc]init];
            com.title = [dic objectForKey:@"title"];
            com.image = [dic objectForKey:@"image"];
            com.releaseDate = [dic objectForKey:@"releaseDate"];
            com.director = [dic objectForKey:@"director"];
            com.actor1 = [dic objectForKey:@"actor1"];
            com.actor2 = [dic objectForKey:@"actor2"];
            com.type = [dic objectForKey:@"type"];
            com.wantedCount = [[dic objectForKey:@"wantedCount"]integerValue];
            com.videoId = [[dic objectForKey:@"videoCount"]integerValue];
            com.movId = [[dic objectForKey:@"id"]integerValue];
            [self.array addObject:com];
            [com release];
            
            
            if (WIDTH == 320 && HEIGHT == 568) {
                [self creatScroll5];
            }
            if (WIDTH == 414 && HEIGHT == 736) {
                [self creatScrollPlus];
            }
            if (WIDTH == 375 && HEIGHT == 667){
                [self creatScroll6];
            }

            

            
            
            
            self.title.text = com.title;
            NSString *st = [com.releaseDate substringToIndex:4];
            self.releaseDate.text = st;
            NSString *str = com.director;
            self.director.text = [NSString stringWithFormat:@"导演:%@",str];
            NSString *type = [NSString stringWithFormat:@"-  %@", com.type];
            self.typeLabel.text = type;
            NSString *str1 = com.actor1;
            NSString *str2 = com.actor2;
            self.actor.text = [NSString stringWithFormat:@"演员:%@,%@",str1,str2];
            NSURL *url = [NSURL URLWithString:com.image];
            [self.image sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"111111.png"]];
            self.wantedCount.text = [NSString stringWithFormat:@"%ld",com.wantedCount];
            
        }
        
//        NSString *first = [self.array firstObject];
//        NSString *last = [self.array lastObject];
//        [self.array insertObject:last atIndex:0];
//        [self.array addObject:first];
//        [self createTimer];
         self.scrollView.contentSize = CGSizeMake(WIDTH*(self.array.count), 0);
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        //        [self.HUD hide:YES];
       
        
        
    }];
    
}




@end
